package com.example.myapplication.base

import com.example.sihookuiklydemo.base.BasePager
import com.example.sihookuiklydemo.base.MySwitch
import com.example.sihookuiklydemo.base.NotifyName
import com.example.sihookuiklydemo.ble.ADDRESS_DESK_TIMER_GET
import com.example.sihookuiklydemo.ble.getNewFloatPx
import com.example.sihookuiklydemo.ble.readData
import com.example.sihookuiklydemo.ble.sendNotifyList
import com.example.sihookuiklydemo.ble.setTimerDuration
import com.example.sihookuiklydemo.utils.getLanguage

import com.tencent.kuikly.core.base.Animation
import com.tencent.kuikly.core.base.Color
import com.tencent.kuikly.core.base.ComposeView
import com.tencent.kuikly.core.base.ComposeAttr
import com.tencent.kuikly.core.base.ComposeEvent
import com.tencent.kuikly.core.base.Translate
import com.tencent.kuikly.core.base.ViewBuilder
import com.tencent.kuikly.core.base.ViewContainer
import com.tencent.kuikly.core.base.event.EventHandlerFn
import com.tencent.kuikly.core.directives.vforIndex
import com.tencent.kuikly.core.directives.vif
import com.tencent.kuikly.core.log.KLog
import com.tencent.kuikly.core.nvi.serialization.json.JSONArray
import com.tencent.kuikly.core.reactive.handler.observable
import com.tencent.kuikly.core.reactive.handler.observableList
import com.tencent.kuikly.core.views.Text
import com.tencent.kuikly.core.views.View

internal class ClockActionSheetView : ComposeView<ActionSheetViewAttr, ClockActionSheetEvent>() {
    var animated: Boolean by observable(false)
    var isOpen: Boolean by observable(false)
    var selectedIndex by observable(-1)
    var didFirstLayout = false
    private var list by observableList<Int>()
    override fun created() {
        super.created()
        list.add(30)
        list.add(60)
        list.add(90)
        list.add(120)
        list.add(150)
        isOpen = attr.currentClockValue != 0
        selectedIndex = list.indexOf(attr.currentClockValue)
    }

    override fun createEvent(): ClockActionSheetEvent {
        return ClockActionSheetEvent()
    }

    override fun createAttr(): ActionSheetViewAttr {
        return ActionSheetViewAttr()
    }

    override fun body(): ViewBuilder {
        val ctx = this
        return {
            attr {
                absolutePosition(0f, 0f, 0f, 0f)
                justifyContentFlexEnd()
                if (ctx.animated) {
                    backgroundColor(Color(0, 0, 0, 0.5f))
                } else {
                    backgroundColor(Color(0, 0, 0, 0f))
                }
                animation(Animation.springEaseIn(0.5f, 0.92f, 1f), ctx.animated)


            }
            event {
                click {
                    ctx.animated = false
                }
                animationCompletion {
                    if (!ctx.animated) {
                        KLog.i("ActionSheetView", "animationCompletion $it")
                        this@ClockActionSheetView.emit(ClockActionSheetEvent.CLOSE, it);
                    }
                }
            }
            View {
                View {
                    attr {

                        alignSelfCenter()
                        backgroundColor(0xffe0e2e7)
                        size(40f, 3f)
                        marginTop(6f)
                        borderRadius(2f)
                    }
                }
                attr {
                    backgroundColor((getPager() as BasePager).theme.colors.background)
                    paddingBottom(30f)
                    borderRadius(16f, 16f, 0f, 0f)
                    if (ctx.animated) {
                        transform(Translate(0f, 0f))
                    } else {
                        transform(Translate(0f, 1f))
                    }
                    animation(Animation.springEaseIn(0.5f, 0.92f, 1f), ctx.animated)
                }
                event {
                    click { }
                }
                Text {
                    attr {
                        text(getLanguage("desk_alarm_reminder","闹铃提醒"))
                        useDpFontSizeDim(true)
                        fontSize(getNewFloatPx(16f))
                        marginTop(getNewFloatPx(16f))
                        alignSelfCenter()
                        color((getPager() as BasePager).theme.colors.mainTextColor)

                    }
                }
                View {
                    attr {
                        marginLeft(getNewFloatPx(16f))
                        flexDirectionColumn()
                    }
                    Text {
                        attr {
                            marginTop(getNewFloatPx(22f))
                            fontSize(getNewFloatPx(15f))
                            text(getLanguage("desk_alarm_reminder","闹铃提醒"))
                            color((getPager() as BasePager).theme.colors.mainTextColor)

                            useDpFontSizeDim(true)
                        }

                    }
                    Text {
                        attr {
                            useDpFontSizeDim(true)
                            text(getLanguage("desk_alarm_reminder_sub","当到达定时时间，蜂鸣器响5声，手控器自动唤醒"))
                            marginTop(getNewFloatPx(2f))
                            fontSize(getNewFloatPx(12f))
                            color((getPager() as BasePager).theme.colors.mainTextColor)

                            opacity(0.4f)
                        }
                    }

                }
                MySwitch {
                    attr {
                        positionAbsolute()
                        right(getNewFloatPx(16f))
                        top(getNewFloatPx(54f))
                        isOn(ctx.isOpen)

                        marginRight(getNewFloatPx(16f))
                    }
                    event {
                        switchOnChanged {
                            KLog.i("switch", "$it")
                            ctx.isOpen=it
                            // ctx.sendNotify("lock", it)
                            var clockTime=0
                            if (it) {
                                var sendIndex=if(ctx.selectedIndex==-1) 0 else ctx.selectedIndex
                                ctx.selectedIndex=sendIndex
                                clockTime=ctx.list[sendIndex]
                            }else{
                                ctx.selectedIndex=-1
                            }
                            var jsonArray = JSONArray()
                            jsonArray.put(setTimerDuration(clockTime).toList())
                            jsonArray.put(readData(ADDRESS_DESK_TIMER_GET).toList())
                            sendNotifyList(ctx, NotifyName.SEND_VALUE_ARRAY, jsonArray)
                        }
                    }
                }
                View {
                    attr {
                        flexDirectionRow()
                        flexWrapWrap()
                        flex(1f)
                        marginTop(getNewFloatPx(16f))
                        marginLeft(getNewFloatPx(8f))

                    }
                    vforIndex({ ctx.list }) { item, index, count ->
                        View {
                            attr {
                                size((ctx.pagerData.pageViewWidth -76) / 3f, 45f)
                                if (index == ctx.selectedIndex) {
                                    backgroundColor(Color.RED)

                                    var jsonArray = JSONArray()
                                    jsonArray.put(setTimerDuration(item).toList())
                                    jsonArray.put(readData(ADDRESS_DESK_TIMER_GET).toList())
                                    sendNotifyList(ctx, NotifyName.SEND_VALUE_ARRAY, jsonArray)
                                } else {
                                    backgroundColor((getPager() as BasePager).theme.colors.grey_1a)
                                }

                                marginTop(8f)
                                marginLeft(8f)
                                marginRight(8f)
                                allCenter()
                                borderRadius(30f)
                            }
                            event {
                                click {
                                    ctx.selectedIndex = index
                                }
                            }
                            Text {
                                attr {
                                    var label=getLanguage("common_minute","分钟")
                                    text("${item}$label")
                                    fontSize(15f)
                                    useDpFontSizeDim(true)
                                    textAlignCenter()
                                    alignSelfCenter()
                                    if (index == ctx.selectedIndex) {
                                        color(Color.WHITE)
                                    } else {
                                        color((getPager() as BasePager).theme.colors.mainTextColor)
                                    }
                                }


                            }
                        }


                    }
                    vif({!ctx.isOpen}){
                        View {
                            event {
                                click {  }
                            }
                            attr {
                                positionAbsolute()
                                absolutePosition(0f,0f,0f,0f)
                                backgroundColor((getPager() as BasePager).theme.colors.background)
                                opacity(0.6f)

                            }
                        }
                    }

                }
                View {
                    attr { height(180f) }
                }
            }


        }
    }

    override fun viewDidLayout() {
        super.viewDidLayout()
        animated = true
    }


}


internal class ActionSheetViewAttr : ComposeAttr() {
    var currentClockValue: Int = 0
}

internal class ClockActionSheetEvent : ComposeEvent() {
    fun close(handler: EventHandlerFn) {
        registerEvent(CLOSE, handler)
    }

    companion object {
        const val CLOSE = "close"
    }
}

internal fun ViewContainer<*, *>.ClockActionSheetEvent(init: ClockActionSheetView.() -> Unit) {
    addChild(ClockActionSheetView(), init)
}